package com.qyer.search.utils;

import com.qyer.search.cache.mutable.MutableCaches;
import com.qyer.search.model.mutable.Place;
import com.qyer.search.model.mutable.StrictPlace;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
/**
 * Created by tangzheng on 16/8/11.
 */
public class DestinationUtils {
  public static List<Place> getDestination(String city, String country, String continent){

    List<Place> places = new ArrayList<>();
    int countryId = -1;
    int cityId = -1;
    int continentId = -1;
    if(StringUtils.isNotBlank(city)){
      List<StrictPlace> strictPlaces = MutableCaches.getInstance().STRICT_PLACE_MAP.getCacheContent().get(city);
      if(strictPlaces != null) {
        if (strictPlaces.size() == 1) {
          StrictPlace strictPlace = strictPlaces.get(0);
          cityId = strictPlace.getCityId();
          countryId = strictPlace.getCountryId();
          continentId = MutableCaches.getInstance().COUNTRY_CONTINENT_ID_MAP.getCacheContent()
                                                                            .get(countryId);
          Place place = new Place(continentId, countryId, cityId);
          places.add(place);
        } else {
          if(StringUtils.isEmpty(continent) && StringUtils.isEmpty(country)){
            for(StrictPlace strictPlace:strictPlaces){
              cityId = strictPlace.getCityId();
              countryId = strictPlace.getCountryId();
              continentId = MutableCaches.getInstance().COUNTRY_CONTINENT_ID_MAP.getCacheContent().get(
                countryId);
              Place place = new Place(continentId, countryId, cityId);
              places.add(place);
            }
          }else if(StringUtils.isNotEmpty(country)){
            for(StrictPlace strictPlace:strictPlaces){
              String countryName = MutableCaches.getInstance().INVERSE_COUNTRIES.getCacheContent().get(strictPlace.getCountryId());
              if(country.equals(countryName)){
                cityId = strictPlace.getCityId();
                countryId = strictPlace.getCountryId();
                continentId = MutableCaches.getInstance().COUNTRY_CONTINENT_ID_MAP.getCacheContent().get(
                  countryId);
                Place place = new Place(continentId, countryId, cityId);
                places.add(place);
              }
            }
          }else if(StringUtils.isNotEmpty(continent)){
            int cid = MutableCaches.getInstance().CONTINENT_DICT.getCacheContent().getReverseDict().get(continent);
            for(StrictPlace strictPlace:strictPlaces){
              countryId = strictPlace.getCountryId();
              int ccid= MutableCaches.getInstance().COUNTRY_CONTINENT_ID_MAP.getCacheContent().get(countryId);
              if(ccid == cid){
                cityId = strictPlace.getCityId();
                Place place = new Place(cid, countryId, cityId);
                places.add(place);
              }
            }
          }
        }
      }
    }else if(StringUtils.isNotBlank(country)){
      if(MutableCaches.getInstance().COUNTRIES.getCacheContent().containsKey(country)){
        countryId = MutableCaches.getInstance().COUNTRIES.getCacheContent().get(country);
        continentId = MutableCaches.getInstance().COUNTRY_CONTINENT_ID_MAP.getCacheContent().get(countryId);
        Place place = new Place(continentId, countryId, cityId);
        places.add(place);
      }
    }else if(StringUtils.isNotBlank(continent)){
      continentId = MutableCaches.getInstance().CONTINENT_DICT.getCacheContent().getReverseDict().get(continent);
      Place place = new Place(continentId, countryId, cityId);
      places.add(place);
    }
    return places;
  }
}
